<template>
  <view class="content">
    <u-action-sheet
      :actions="arrUrl"
      :show="show"
      @select="actionClick"
      @close="show = false"
    ></u-action-sheet>
  </view>
</template>

<script setup name="biz-switch-env-config">
import { ref, defineExpose } from 'vue'
import { ApiUrl, developApiUrl, testApiUrl, proApiUrl } from '@/config/api'
import { isProduct } from '@/config'
const show = ref(false)

const openSetEnv = () => (show.value = true)

const actionClick = (item) => {
  let { Fun, apiUrl } = item
  Fun(apiUrl)
}

const changeEnv = (apiUrl) => {
  uni.setStorageSync('CONFIG_API_URL', apiUrl)
  showTips('切换成功')
}

const clearConfigStorage = () => {
  uni.clearStorage()
  showTips('清除成功，')
}

const showTips = (fix = '') => {
  let title = fix + '请点击右上角重新进入小程序'
  // #ifdef H5
  title = fix + '请刷新页面'
  // #endif
  uni.showToast({
    title,
    duration: 5000
  })
}

const arrUrl = ref([
  {
    name: '开发环境' + (ApiUrl === developApiUrl ? '(当前配置)' : ''),
    color: '#999',
    fontSize: '32rpx',
    Fun: changeEnv,
    apiUrl: developApiUrl
  },
  {
    name: '测试环境' + (ApiUrl === testApiUrl ? '(当前配置)' : ''),
    color: '#999',
    fontSize: '32rpx',
    Fun: changeEnv,
    apiUrl: testApiUrl
  },
  {
    name: '线上环境' + (ApiUrl === proApiUrl ? '(当前配置)' : ''),
    color: '#999',
    fontSize: '32rpx',
    Fun: changeEnv,
    apiUrl: proApiUrl
  },
  {
    name: '清除本地缓存',
    color: '#999',
    fontSize: '32rpx',
    Fun: clearConfigStorage
  }
])

defineExpose({
  openSetEnv
})
</script>

<style></style>
